cmake_minimum_required(VERSION 2.8)
project(lsv C)

set(CMAKE_C_FLAGS "-g -w -Wall -Werror -rdynamic -D_GUN_SOURCE -D_REENTRANT -D_FILE_OFFSET_BITS=64 -std=c99 -Dlinux -lrt -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(LSV_HOME "${CMAKE_CURRENT_SOURCE_DIR}")

set(LICH_HOME "${LSV_HOME}/..")
set(FUSE_LINK "${LSV_HOME}/lib/fuse")

include_directories(
    ${LICH_HOME}/ylib/include

    ${LSV_HOME}/lsv/include
    ${LSV_HOME}/lsv/src/bitmap
    ${LSV_HOME}/lsv/src/log
    ${LSV_HOME}/lsv/src/volume
    ${LSV_HOME}/lsv/src/wbuffer
    ${LSV_HOME}/lsv/src/rcache
    )

#link_directories("${LICH_HOME}/cb")
link_directories(
    ${LICH_HOME}/cb
    ${FUSE_LINK}
    #"/home/xingjing/skyfs_ca/client/fuse-2.9.4/lib"
)
# set(LIBS pthread fuse rt dl m)
set(LIBS lich4 fuse dl sqlite3 uuid pthread aio crypt crypto ssl rt m)

function(build_tool name source_files libs)
    add_executable(${name} ${source_files})
    target_link_libraries(${name} ${libs})
endfunction()

add_custom_target(lsv_version ALL
        COMMAND mkdir -p /mnt/lsv-volume
        COMMAND mkdir -p /mnt/lsv-volume/data
        COMMAND mkdir -p /mnt/lsv-volume/meta
        COMMAND mkdir -p /mnt/lsv-volume/log
        )
        
add_custom_target(clear
		COMMAND rm -rf /mnt/lsv-volume/meta/*
		COMMAND rm -rf /mnt/lsv-volume/data/*
		COMMAND rm -rf /mnt/lsv-volume/log/*
		COMMAND umount lsv
        )

set(LVS_FILES
    ${LSV_HOME}/lsv/src/main.c
    ${LSV_HOME}/lsv/src/lsv_op.c
    ${LSV_HOME}/lsv/src/lsv_utils.c
    ${LSV_HOME}/lsv/src/bitmap/lsv_bitmap.c
    ${LSV_HOME}/lsv/src/bitmap/lsv_bitmap_io.c
    ${LSV_HOME}/lsv/src/bitmap/lsv_bitmap_snap.c
    ${LSV_HOME}/lsv/src/bitmap/lsv_bitmap_cache.c
    ${LSV_HOME}/lsv/src/log/lsv_log.c
    ${LSV_HOME}/lsv/src/log/lsv_log_thread.c
    ${LSV_HOME}/lsv/src/log/lsv_log_gc.c
    ${LSV_HOME}/lsv/src/volume/lsv_volume.c
    ${LSV_HOME}/lsv/src/volume/lsv_volume_gc.c
    ${LSV_HOME}/lsv/src/volume/lsv_volume_storage_file_impl.c
    ${LSV_HOME}/lsv/src/wbuffer/lsv_wbuffer_utils.c
    ${LSV_HOME}/lsv/src/wbuffer/lsv_wbuffer_thread.c
    ${LSV_HOME}/lsv/src/wbuffer/lsv_wbuffer.c
    ${LSV_HOME}/lsv/src/rcache/lsv_rcache.c
    )
build_tool(lsv "${LVS_FILES}" "${LIBS}")
